﻿/*******************************************************************************
 * CreateTime:2022-04-04 11:37:53
 * Description: YoursLC有源低代码
*********************************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Table;
using Think9.Services.Flow;

namespace Think9.Services.CodeBuild
{
	/// <summary>
	/// 主表Service
	/// </summary>
	public class CustomerManagementService : BaseService<CustomerManagementModel>
	{
		private ComService ComService = new ComService();
		private string split = BaseUtil.ComSplit;//字符分割 用于多选项的分割等 如split+选项1+split+选项2+split
		private string sql;

		//添加前数据处理
		public int BeforeAdd(ref string err, CurrentUserEntity CurrentUser, CurrentPrcsEntity mPrcs, string flowid, string maintbid, string tbname)
		{
			int listid = 0;

			//处理自动编号-- 如果有则添加一条数据,返回自增长的listid;如果没有则返回0
			listid = AutoNo.SetAutoNumber(ref err, CurrentUser, mPrcs, flowid, tbname);



			return listid;
		}

		//编辑后数据处理
		public string AfterEdit(string listid, string flowid, string prcsid, CurrentUserEntity user)
        {
			string err = "";

			//调用通用函数实现数据回写，用户可自定义 数据回写在录入表管理/数据读写中设置
			WriteBack WriteBack = new WriteBack();
			string maintbid = flowid.Replace("bi_", "tb_").Replace("fw_", "tb_");
			err = WriteBack.WriteValueByRelationSet(listid, flowid, maintbid, prcsid, user);

			return err;
		}

		//完成后数据处理
		public string AfterFinish(string listid, string flowid, string prcsid, CurrentUserEntity user)
		{
			string err = AfterEdit(listid, flowid, prcsid, user);
			if(string.IsNullOrEmpty(err))
            {
				//数据锁定等
				FlowCom.FinishFlowPrcs(listid, flowid, prcsid, user);
            }

			return err;
		}

		/// <summary>
		/// 主表查询调用 返回列表 查询条件可在录入表指标属性中设置
		/// </summary>
		/// <param name="total"></param>
		/// <param name="model"></param>
		/// <param name="pageInfo"></param>
		/// <param name="user"></param>
		/// <param name="flowid"></param>
		/// <param name="tbid"></param>
		/// <returns></returns>
		public IEnumerable<dynamic> GetPageListBySearch(ref long total, CustomerManagementModel model, PageInfoEntity pageInfo, CurrentUserEntity user, string flowid, string tbid)
		{
			string where = BasicHelp.GetWhereByFlowId(user, flowid);

			//编号
			if (!string.IsNullOrEmpty(model.inNumber))
			{
				where += " and (tb_CustomerManagement.inNumber like @inNumber)"; 
				model.inNumber = string.Format("%{0}%", model.inNumber); 
			}
			//客户名称
			if (!string.IsNullOrEmpty(model.inCustomerName))
			{
				where += " and (tb_CustomerManagement.inCustomerName like @inCustomerName)"; 
				model.inCustomerName = string.Format("%{0}%", model.inCustomerName); 
			}
			//联系人
			if (!string.IsNullOrEmpty(model.inContacts))
			{
				where += " and (tb_CustomerManagement.inContacts like @inContacts)"; 
				model.inContacts = string.Format("%{0}%", model.inContacts); 
			}
			//联系电话
			if (!string.IsNullOrEmpty(model.inContactNumber))
			{
				where += " and (tb_CustomerManagement.inContactNumber like @inContactNumber)"; 
				model.inContactNumber = string.Format("%{0}%", model.inContactNumber); 
			}
			//状态
			if (!string.IsNullOrEmpty(model.inState))
			{
				where += " and (tb_CustomerManagement.inState = @inState)"; 
			}
			//备注
			if (!string.IsNullOrEmpty(model.inRemarks))
			{
				where += " and (tb_CustomerManagement.inRemarks like @inRemarks)"; 
				model.inRemarks = string.Format("%{0}%", model.inRemarks); 
			}
			return base.GetPageByFilter(ref total, model, pageInfo, where);

		}

		/// <summary>
		/// 主表 为下拉选择、多选、单选准备动态数据源
		/// 负责列表显示时value与text转化 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="from"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();

			//状态
			sql = "select DetailCode as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'State' ORDER BY OrderNo";
			foreach (DataRow dr in ComService.GetDataTable(sql, param).Rows)
			{
				DataRow row = dt.NewRow();
				row["ClassID"] = "inState";
				row["Value"] = dr["id"].ToString();
				row["Text"] = dr["name"].ToString();
				dt.Rows.Add(row);
			}

			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 弹出页面的查询条件有下拉选择、多选、单选时，为其绑定动态数据源
		/// 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="tbid"></param>
		/// <param name="indexid"></param>
		/// <param name="from"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string tbid, string indexid, string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();

			//主表指标弹出页面
			if (tbid == "_main")
			{

			}
			else//子表指标弹出页面
			{

			}

			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 数据删除
		/// </summary>
		/// <param name="id"></param>
		/// <param name="fwid"></param>
		/// <returns></returns>
		public bool DeleteByID(string id, string fwid)
		{

			AttachmentService.DelAttachment(int.Parse(id), fwid);//删除附件 要最先执行
			//删除flowrunlist、flowrunprcslist中关联数据
			if (fwid.StartsWith("fw_"))
			{
				ComService.ExecuteSql("delete from flowrunlist where ListId = " + id + "");
				ComService.ExecuteSql("delete from flowrunprcslist where ListId = " + id + "");
			}

			AutoNo.DelAutoNo(id, fwid);//删除自动编号

			return base.DeleteByWhere("where ListId=" + id + "");//删除主表数据
		}

		/// <summary>
		///返回可修改字段 自由流程排除了隐藏指标 固定流程排除了隐藏指标及不可写字段
		/// </summary>
		/// <param name="prcno"></param>
		/// <returns></returns>
		public string GetUpdateFields(string prcno)
		{
			//自由流程或无流程排除了隐藏指标
			 return "inNumber,inCustomerName,inContacts,inContactNumber,inState,inRemarks";
		}


	}
}